package game;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.lcdui.*;
import javax.microedition.m2g.*;

/**
 * This class handles all things which have to be done for a pretty nice gameover_splashscreen!
 * @author Gustav
 * @since 02.6.2010
 */
public class GameOverScreen {
    private SVGImage svgImage;
    private SVGAnimator svgAnimator;
    private Canvas svgCanvas;
    private MIDPSVGEventListenerGameOver listener;

    /**
     * inner class... just handles non-vectorized images
     */
    class ImageHandler implements ExternalResourceHandler {
        public void requestResource(final ScalableImage scalableImage, final String URI){
            final InputStream inputStream = Image.class.getResourceAsStream(URI);

            if (inputStream == null)
                System.out.println("Failed to load " + URI);

            try{
                scalableImage.requestCompleted(URI, inputStream);
            }
            catch (final IOException e){
                e.printStackTrace();
            }
        }
    }

    /**
     * One-Time initialization
     */
    public GameOverScreen(){
        InputStream is = getClass().getResourceAsStream("/gameover.svg");
        try {
                 svgImage = (SVGImage)SVGImage.createImage(is, new ImageHandler());
        } catch (IOException e) {
                e.printStackTrace();
            System.out.println("Could not load image!");
        }
        svgAnimator = SVGAnimator.createAnimator(svgImage);
        listener = new MIDPSVGEventListenerGameOver(svgAnimator, svgImage);
        svgAnimator.setSVGEventListener(listener);
        svgCanvas = (Canvas)svgAnimator.getTargetComponent();
        svgCanvas.setFullScreenMode(true);
    }

    /**
     * Wanna see the splash? You wanna see the splash!
     */
    public void start(){
        listener.init();
        svgAnimator.play();
        Display.getDisplay(LUMM2_Canvas.midlet).setCurrent(svgCanvas);
    }
}
